package main

import (
	"fmt"
	"learngo/retriever/mock"
	real2 "learngo/retriever/real"
	"time"
)

type Retriever interface {
	Get(url string) string
}

type Poster interface {
	Post(url string,
		form map[string]string) string
}

func download(r Retriever) string {
	return r.Get("http://www.imooc.com")
}

func post(poster Poster) {
	poster.Post("http://www.imooc.com",
		map[string]string{
			"name":   "ccmouse",
			"course": "golang",
		})
}

type RetrieverPoster interface {
	Retriever
	Poster
}

const url = "http://www.imooc.com"

func session(s RetrieverPoster) string {
	s.Post(url, map[string]string{
		"contents": "another faked.imooc.com",
	})
	return s.Get(url)
}

func main() {
	var r Retriever

	//接口可以存储值
	r = &mock.Retriever{"this is a fake imooc com"}
	//fmt.Println(download(r))
	inspect(r)

	//接口可以存储指针
	r = &real2.Retriever{"Mozilla/5.0", time.Minute}

	//inspect(r)

	//Type assertion
	realRetriever := r.(*real2.Retriever)
	fmt.Println(realRetriever.Timeout)

	//fmt.Println(download(r))

	fmt.Println("try a session")
	retriever := &mock.Retriever{"this is a fake imooc com"}
	fmt.Println(session(retriever))
}

func inspect(r Retriever) {
	fmt.Println("Inspecting", r)
	fmt.Printf(" > %T %v\n", r, r)
	switch v := r.(type) {
	case *mock.Retriever:
		fmt.Println("> Contents:", v.Contents)
	case *real2.Retriever:
		fmt.Println("> UserAgent:", v.UserAgent)
	}
	fmt.Println()
}
